<template>
  <a-card :bordered="false">
    <a-row type="flex" justify="space-between" slot="title"
      >{{ title }}<a-button @click="handleSave" type="primary" icon="plus">保存</a-button></a-row
    >
    <a-form ref="form" style="margin-top: 8px">
      <a-form-item :labelCol="{ span: 4 }" :wrapperCol="{ span: 20 }" label="日期">
        <a-range-picker
          :default-value="defaultValue"
          @change="onChange"
        />
      </a-form-item>
    </a-form>
  </a-card>
</template>

<script>
import moment from 'moment'
import { postAction } from '@/api/manage'
export default {
  name: 'TimeRangeConfig',
  props: {
    title: String,
    value: [String, Number],
    bizId:String
  },
  data(){
      return {
          url:{
              save:'/bizConfig/add '
          },
          dates:[]
      }
  },
  computed:{
    defaultValue(){
      let arr = this.value.split('-')
      return [moment(parseInt(arr[0])||''),moment(parseInt(arr[1])||'')]
    }
  },
  methods: {
    onChange(dates, dateStrings) {
        this.dates = dates
    },
    handleSave() {
        let that = this
        if(!that.dates[0]){
            return that.$message.warning('请选择开始日期');
        }
        if(!that.dates[1]){
            return that.$message.warning('请选择结束日期');
        }
        let params ={
            id:that.bizId,
            value: `${moment(that.dates[0]).valueOf()}-${moment(that.dates[1]).valueOf()}`
        }
        postAction(that.url.save, params).then(res => {
            if(res.success){
                that.$message.success(res.message);
            }else{
                that.$message.warning(res.message);
            }
        })
    }
  }
}
</script>

<style></style>
